
closeDiv(); // 关闭加载
$('#bingzhongxx').focus();
var cxzhuangtai = '';//查询状态
// 获取页面参数
var pageNumber = 1;
var dingweiid ;
var dwpanduan = false;
function getJiBingmlcs(preYeMiancs) {
	var YeMiancs = JSON.parse(preYeMiancs);
	if(YeMiancs != null) {
		pageNumber = YeMiancs.pageNo;
		dingweiid = YeMiancs.id;
		dwpanduan = true;
	}
	getBingzhongxx();
}

//病种信息数据显示列表
function getBingzhongxx() {
	var totalNumber = 0;
	switch($('#zhuangtai span').text()) {
	case '全部': cxzhuangtai = ''
		break;
	case '禁用': cxzhuangtai = 1
		break;
	case '启用': cxzhuangtai = 0
		break;
	};
	var tiaojian = $('#bingzhongmc-icd10').val();
	yibaobzbm = '';
	var sendMsg = {
			'jinyongbz': cxzhuangtai,
			'bingzhongmloricd10': tiaojian,
			'yibaobzbm': yibaobzbm,
			'rukou': '1'
			};
	$.ajax({
		url: './bingzhongml/getBingzhongCount',
		type: 'post',
		data: sendMsg,
		async: false,
		success: function(data){
			if(data != null){
				totalNumber = data;
			}
		},
		error: function(){
				console.log("请求失败");
		}
	});
	$('#tbbingzhongml').html('');
	var container = $('#pagination-fenybingzhong');
	container.pagination({
	dataSource: './bingzhongml/getBingzhongList?jinyongbz=' + cxzhuangtai + '&bingzhongmloricd10=' + tiaojian
	+ '&yibaobzbm=' + yibaobzbm + '&rukou=1',
	locator: '',
	pageSize: 12,
	totalNumber: totalNumber,
	 //增加总记录数
	showNavigator: true,
	pageNumber: pageNumber,
    formatNavigator: '共 <%= totalNumber %> 条',
	callback: function(data, pagination){
		$("#tbbingzhongml").html("");
        	  $.each(data, function(i, v){
             	    var perPageRecordNum = pagination.pageSize; //每页数据行数
            		var pageNo = pagination.pageNumber; //当前页号
            		var index = (((pageNo-1) * perPageRecordNum) + i + 1);
            		var content = "<tr id='jibingh" + this.id + "'>"
		    				+ "<td width='8%'><label class='label'>" + (((pageNo - 1) * perPageRecordNum) + i + 1) + "</label></td>"
		     	 			+ "<td width='25%' style='overflow: visible;position: relative;'><input type='text' class='dropdown-toggle input-sm col-sm-12 u-input bzxx' id='bingzhong' name='bingzhong' /><ul id='' class='xialaxuan dropdown-menu li-width f-input-li bingzhong'  style='display:none;top: 36px; padding:0;left: 3px;border-radius: 0;width:198%;' role='menu' ></ul></td>"
		     	 			+ "<td width='25%'><input type='text' shuxinglb='' class='input-sm col-sm-12 u-input' id='icd-10' name='icd-10' /></td>"
		     	 			+ "<td width='15%' style='overflow: visible;'>" 
		     	 			+ '<div class="menu-up choose dropdown-search-li none-border" style="position: relative;">' 
		     	 			+ '<input type="hidden" id="bingzhongxx-zt" value="0">' 
		     	 			+ '<div  class="btn dropdown-toggle select-menu" data-toggle="dropdown" tabindex="0" id="zhuangtai-xianshi"><span></span></div>' 
		     	 			+ '<ul class="dropdown-menu lbzhuangtai" role="menu" id="lbzhuangtai"><li value="0">禁用</li><li value="1">启用</li></ul></div>' 
		     	 			+ "</td>" 
		             		+ '<td width="15%"><a class="operation-left"  id="bingzhongml-quedin" onclick="baocun(' + data[i].id + ',' + (((pageNo - 1) * perPageRecordNum) + i + 1) + ')">保存</a>'
		             		+ '</td>'
		             		+ "</tr>";
            		$("#tbbingzhongml").append(content);
            		//内容读取
 	                $("#jibingh"+this.id).find("#bingzhong").val(data[i].jibingmc);
 	                //读取icd10
 	                $("#jibingh"+this.id).find("#icd-10").val(data[i].icd10 || data[i].zhongyihzdm);
 	                 // 禁启数据读取
 					$("#jibingh"+this.id).find("#bingzhongxx-zt").val(data[i].jinyongbz);
 					if(data[i].jinyongbz == 0){
 						var text = '启用';
 					}else{
 						var text = '禁用';
 					}
 					$("#jibingh"+this.id).find("#zhuangtai-xianshi span").html(text);
             });
        	  // 定位判断
        	  if(dwpanduan) {
	        	  $('#tbbingzhongml').find('tr[id="jibingh'+dingweiid+'"]').addClass('list-tr');
	        	  $('#tbbingzhongml').find('tr[id="jibingh'+dingweiid+'"] input').addClass('list-tr');
        	  }
         }
     });
	if(totalNumber <= 12) {
		$('#pagination-fenybingzhong').css('display','none');
	}else{
		$('#pagination-fenybingzhong').css('display','block');
	}
};

$(document).on("blur","#bingzhongxx",function(){
	if($(this).hasClass("f-jiaoyan")){
		if($(this).val() != ''){
			$(this).removeClass("f-jiaoyan");
			$(this).addClass("input-border");
		}
	}
});

//查询按钮
$('#bingzhongcx').click(function(){
	pageNumber = 1;
	dwpanduan = false;
	getBingzhongxx();
	return false;
});

//选择查询状态
$("#cxzhuangtai li").click(function(){
	$('#zhuangtai span').text($(this).text());
	$('#cxzhuangtai').css("display","none");
	dwpanduan = false;
	pageNumber= 1;
	getBingzhongxx();
});

//选择添加病种信息状态
$('#tianjia-zt li').click(function(){
	$('#zhuangtai-add span').text($(this).text());
	$('#tianjia-zt').css("display","none");
});

// 修改病种信息处的选择要保存的状态
$(document).on('click','.lbzhuangtai li',function() {
	$(this).parent().parent().find('span').text($(this).text());
	$(this).parent().css("display","none");
	if($(this).text() == '启用'){
		$(this).parent().parent().find('input').val('0');
	} else {
		$(this).parent().parent().find('input').val('1');
	}
});

//点击添加按钮
$('#bingzhongxx-add').click(function(){
	dwpanduan = false;
	var jiaoyan = true;
	if($('#bingzhongxx').val().length == 1){
		
		if($('#bingzhongxx').val().match(/[^\x00-\xff]/ig) != null){
			jiaoyan = true;
		}else{
			notifyWarning("病种名称太短，请重新输入");
			$(this).parent().parent().find('.bzxx').addClass("n-invalid");
			jiaoyan = false;
			return false;
		}
		
	}else{
		$(this).removeClass("n-invalid");
	}
	var bingzhongxx = $('#bingzhongxx').val();
	var icd10 = $('#icd10').val();
	var zhuangtai = 0;
	switch($('#zhuangtai-add').text()){
	case "启用":zhuangtai = 0;
		break;
	case "禁用":zhuangtai = 1;
		break;
	};
	if($('#icd10').val().length > 63){
		notifyWarning("ICD-10不能超过64个字符");
		$('#icd10').addClass("n-invalid");
		jiaoyan = false;
		return false;
	}
	if($('#bingzhongxx').val().length > 127){
		  notifyWarning("病种信息不能超过128位");
		  $('#bingzhongxx').addClass("n-invalid");
		  jiaoyan = false;
		  return false;
	}else if($('#bingzhongxx').val() == ''){
		  notifyWarning("病种信息不能为空");
		  $('#bingzhongxx').addClass("n-invalid");
		  jiaoyan = false;
		  return false;
	}else{
		jiaoyan = true;
		$(this).removeClass("n-invalid");
	}
	
	if(jiaoyan == true){
		var add_neirong = $("#bingzhongxx").val();
		var icd10 = '';
		var zhongyihzdm = '';
		if ($('#icd10').attr('shuxinglb') == 'icd10') {
			icd10 = $('#icd10').val();
		} else {
			zhongyihzdm = $('#icd10').val();
		}
		var add_zt = '2';
		if($("#zhuangtai-add span").text() == '启用'){
			add_zt = '0';
		}
		if($("#zhuangtai-add span").text() == '禁用'){
			add_zt = '1';
		}
		var sendData ={
				"neirong": add_neirong,
				"icd10": icd10,
				"jinyongbz": add_zt,
				"zhongyihzdm": zhongyihzdm,
				"id": 0
		};
		$.ajax({
			url: './bingzhongml/saveBingzhong',
			type: 'post',
			data: sendData,
			success: function(data){
				if(!!data){
					notifySuccess("保存成功");
					getBingzhongxx();
					$('#bingzhongxx').val('');
					$('#icd10').val('');
					$('#bingzhongxx').removeClass('f-jiaoyan');
					$('#zhuangtai-add').text("");
					var $span = $("<span></span>");
					$('#zhuangtai-add').append($span);
					$('#zhuangtai-add span').text("启用");
				}else{
					notifyWarning("该病种已经存在了");
				}
			},
			error: function(){
				notifyWarning("保存失败");
			}
		});
	}
	$('#xialaxuan').html('');
	$('#bingzhongxx').focus();
});

//添加重置按钮
$('#chongzhi-add').click(function(){
	$('.input-sm').removeClass('f-jiaoyan');
	$('#bingzhongxx').val('');
	$('#icd10').val('');
	$(this).parent().parent().find('.bzxx').removeClass("n-invalid");
	$(this).parent().parent().find('.icdten').removeClass("n-invalid");
	$('#zhuangtai-add').text("");
	var $span = $("<span></span>");
	$('#zhuangtai-add').append($span);
	$('#zhuangtai-add span').text("启用");
});
//大的重置按钮
$('#chongzhi').click(function(){
	dwpanduan = false;
	pageNumber = 1;
	$('#zhuangtai').text("");
	var $span = $("<span></span>");
	$('#zhuangtai').append($span);
	$('#zhuangtai span').text("启用");
	$('#bingzhongmc-icd10').val('');
	getBingzhongxx();
});

var xiugaijiaoyan = true;
//修改病种信息字符过长
$(document).on("input", "#icd10", function () {
	xiugaijiaoyan = true;
	if ($(this).val().length > 64) {
		notifyWarning("ICD-10不能超过64个字符");
		$(this).addClass("n-invalid");
		xiugaijiaoyan = false;
		return false;
	}else{
		$(this).removeClass("n-invalid");
	}
});
$(document).on("input", "#icd-10", function () {
	xiugaijiaoyan = true;
	if ($(this).val().length > 64) {
		notifyWarning("ICD-10不能超过64个字符");
		$(this).addClass("n-invalid");
		xiugaijiaoyan = false;
		return false;
	}else{
		$(this).removeClass("n-invalid");
	}
});
//修改病种信息字符不能为空
$(document).on("input", ".Tb-list .bzxx", function () {
	xiugaijiaoyan = true;
	if ($(this).val().length == 0) {
		notifyWarning("病种信息不能为空");
		$(this).addClass("n-invalid");
		xiugaijiaoyan = false;
		return false;
	}else{
		$(this).removeClass("n-invalid");
	}
});

//保存按钮
function baocun(id,index){
	var jiaoyan = true;
	var bingzhongmc = $("#jibingh" + id).find('#bingzhong').val();
	var baocunzt = $("#jibingh" + id).find('#bingzhongxx-zt').val();
	var icd10 = '';
	var zhongyihzdm = '';
	if ( $("#jibingh" + id).find('#icd-10').attr('shuxinglb') == 'icd10') {
		icd10 =  $("#jibingh" + id).find('#icd-10').val();
	} else {
		zhongyihzdm =  $("#jibingh" + id).find('#icd-10').val();
	}
	if(bingzhongmc.length > 127){
		notifyWarning("病种信息不能超过128位");
		$(this).addClass("n-invalid");
		return false;
	}else{
		$(this).removeClass("n-invalid");
	}
	if(icd10.length > 63){
		notifyWarning("ICD-10不能超过64个字符");
		$(this).addClass("n-invalid");
		return false;
	}else{
		$(this).removeClass("n-invalid");
	}
//	var sendMsg = {
//			"id": id,
//			"bingzhongxx": bingzhongmc,
//			"icd10": icd10,
//			"jinyongbz": baocunzt
//	};
//	if(baocunzt == '0'){
//		$.ajax({
//			url: './bingzhongml/compareBingzhongxx',
//			type: 'post',
//			data: sendMsg,
//			async: false,
//			success: function(data){
//				if(data != null && data != '' && id != 0){
//					notifyWarning("该病种已存在");
//					jiaoyan = false;
//				}
//			}
//		});
//	}
	if(jiaoyan == true){
		var sendData = {
				"id": id,
				"bingzhongmc": bingzhongmc,
				"icd10": icd10,
				"baocunzt": baocunzt,
				"zhongyihzdm": zhongyihzdm
		};
		$.ajax({
			url: './bingzhongml/updateBingzhongxx',
			type: 'post',
			data: sendData,
			success: function(data){
				if(!!data){
					notifySuccess("保存成功");
					getBingzhongxx();
				}else{
					notifyWarning("该病种已存在");
				}
				
			},
		});
	}
};

var bingzhongFlag;
//修改病种信息时触发的下拉选

$(document).on('input','.bzxx',function(){
	if($(this).val() == ''){
		$(this).next().html('');
	}
});

$(document).on('input', '.bzxx', function () {
	if ($(this).val() == '') {return false;}
	clearTimeout(Flag);
	var Flag = setTimeout(function(){
		if($('.bzxx').val().length == 1){
			if((/[a-zA-Z1-9]/).test($('.bzxx').val())){
				notifyInfo("与" + $('.bzxx').val()+"匹配的信息过多，提供更多的检索信息，系统将继续为您查找");
				return false;
			}else{
				return false;
			}
		}
	},1800);
	
	if($(this).val().length == 1 && (/[a-zA-Z1-9]/).test($(this).val())){
		$(this).next().html('');
		return false;
	}

	if($(this).val().length > 127){
		notifyWarning("病种信息不能超过128位");
		$(this).addClass("n-invalid");
		var text = $(this).val().substring(0,128);
		$(this).val(text);
	}else{
		$(this).removeClass("n-invalid");
	}
	var msg = $(this).val();
	var sendData = {
			"jiansuoma": msg
	};
	if(msg == '' || msg == null){
		$('.xialaxuan').html('');
	}
	var contact = '';
	var obj = this;
	if(msg != '' && msg != null){
		clearTimeout(bingzhongFlag);
		bingzhongFlag = setTimeout(function(){
			$.ajax({
				url: './bingzhongml/getBingzhongByJsm',
				type: 'post',
				data: sendData,
				async: false,
				success: function(data){
					$('.xialaxuan').html('');
					$.each(data,function(n,val){
						if(val.icd10 == null){
							val.icd10 = '';
						}
						if(val != msg){
							contact += "<li jibingmc='"+val.jibingmc+"' icd10='"+val.icd10+"'zhongyihzdm='"+val.zhongyihzdm+"'>"+val.jibingmc+"&nbsp;&nbsp;&nbsp;"+val.icd10+"</li>";
						}
					});
					$(obj).parent().find('ul').html("");
					$(obj).parent().find('ul').append(contact);
					$(obj).parent().find('ul').css('display','block');
				}
			});
		},200);
	}
});

//修改病种信息时触发下拉选
$('.u-input').click(function(){
	$(this).parent().find('ul').css('display','block');
});

//选中下拉选中的值填入病种信息这个框中
$(document).on('click','#xialaxuan li',function(){
	$('#bingzhongxx').unbind("input");
	$('#bingzhongxx').val($(this).attr("jibingmc"));
	var icd10 = $(this).attr("icd10");
	var zhongyihzdm = $(this).attr("zhongyihzdm");
	if (!icd10) {
		$('#icd10').val(zhongyihzdm).attr("shuxinglb", "zhongyihzdm");
	} else {
		$('#icd10').val(icd10).attr("shuxinglb", "icd10");
	}
	$(this).parent().css("display","none");
	$(this).parent().html('');
});

//用下拉选修改病种信息
$(document).on('click','.bingzhong li',function(){
	var icd10 = $(this).attr("icd10");
	var zhongyihzdm = $(this).attr("zhongyihzdm");
	if (!icd10) {
		$(this).parent().parent().next().find('input').val(zhongyihzdm).attr("shuxinglb", "zhongyihzdm");
	} else {
		$(this).parent().parent().next().find('input').val(icd10).attr("shuxinglb", "zhongyihzdm");
	}
	$(this).parent().prev().val($(this).attr("jibingmc"));
	$(this).parent().css("display","none");
});
